import * as api from '@/apis/modules/login.js';
import {uniHistory} from '@/common/utils.js'

const login = {
	namespaced: true,
	state: {
		agentInfo: {}
	},
	mutations: {
		save(state, {field, payload}) {
		  if (Object.prototype.toString.call(payload) === '[object Object]') {
				state[field] = { ...state[field], ...payload }
		  } else {
				state[field] = payload
		  }
		}
	},
	actions: {
		login({ commit }, payload) {
			api.login(payload).then(res => {
				if (res.error) return
				
				uni.setStorageSync('userInfo', res)
				uniHistory.replace('/pages/home/home')
			}).catch(err => console.log(err))
			
		},
	}
}

export default login